function [cont_current,cont_next, data]=Context(A,prev,threshold, Maxw,Maxh);
% A is processed matrix
%prev - context values obtained from previous (smaller matrix)
%Maxw-maximal width of image component
%Maxh -maximal height of image component
%cont_next -context values for next(larger matrix)
[m,n]=size(A);
%colormap(gray(256));
%image(A);
%pause
%A
%pause
data=zeros(1,m*n);%elements of A scanned by rows 
cont_current=zeros(1,m*n);% context values obtained from current matrix

%data stream forming
B=A';
data=B(:)';
%pause
%current context forming
B=zeros(m,n);
AA=abs(A);
B(1,:)=[0 AA(1,1:n-1)];
B(:,1)=[0 AA(1:m-1,1)']'; 
B(2:m,2:n)=AA(1:m-1,2:n)+AA(2:m,1:n-1)+AA(1:m-1,1:n-1);
B=B';
cont_current=B(:)';
cont_current=cont_current+prev;
%pause
%Quantization of current context 
 for i=1:length(cont_current)
     if cont_current(i)>threshold
         cont_current(i)=threshold;
     end;
 end;
 
 if (m<=Maxw/2 & n<=Maxh/2)
     m=2*m; n=2*n;
     cont_next=zeros(m,n);
     cont_next(1:2:m,1:2:n)=A;
     cont_next(2:2:m,1:2:n)=A;
     cont_next(1:2:m,2:2:n)=A;
     cont_next(2:2:m,2:2:n)=A;
 end;
 %cont_next
 %pause
cont_next=abs(cont_next');
cont_next=cont_next(:)';
%pause


